package com.bnq.control.common.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 判断连接网络的工具类
 * Created by qianwei on 2017/1/13.
 */

public class NetWorkUtils {

    private NetWorkUtils() {
    }

    /**
     * 判断是否有可用网络
     *
     * @param context
     *            上下文
     * @return true 有可用网絡.
     */
    public static boolean hasActiveNet(Context context) {
        boolean flag = false;
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        if (info != null) {
            flag = manager.getActiveNetworkInfo().isAvailable();
        }
        return flag;
    }

    /**
     * 判断wifi是否有效(连接到wifi热点)
     *
     * @return true wifi 有效.
     */
    public static boolean isUsingWifi(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = manager.getActiveNetworkInfo();

        if (null != activeNetwork && activeNetwork.isConnectedOrConnecting()
                && activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            return true;

        return false;
    }
}
